package com.nari.ncps.screen.service.impl.ne;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.ne.NeProdoperPowerGs;
import com.nari.ncps.screen.mapper.ne.NeOpermonEqpStateMapper;
import com.nari.ncps.screen.service.ne.NeOpermonEqpStateService;
import com.nari.ncps.screen.vo.ne.NeEquipmentOverviewVO;
import com.nari.ncps.screen.entity.ne.NeOpermonEqpState;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 新能源设备状态监测表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeOpermonEqpStateServiceImpl extends ServiceImpl<NeOpermonEqpStateMapper, NeOpermonEqpState> implements NeOpermonEqpStateService {

    @Override
    public NeEquipmentOverviewVO getEquipmentOverview(Integer year) {
        // 如果没有指定年份，使用当前年份
        if (year == null) {
            year = LocalDate.now().getYear();
        }
        
        // 查询指定年份的最新数据
        LambdaQueryWrapper<NeOpermonEqpState> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NeOpermonEqpState::getYear, year).eq(NeOpermonEqpState::getOrgCode, "100360");
        queryWrapper.eq(NeOpermonEqpState::getDelFlag, 0);
        queryWrapper.orderByDesc(NeOpermonEqpState::getMonth);
        queryWrapper.last("LIMIT 1");
        
        NeOpermonEqpState entity = this.getOne(queryWrapper);
        
        // 构建返回VO
        NeEquipmentOverviewVO vo = new NeEquipmentOverviewVO();
        if (entity != null) {
            vo.setWindTurbineTotal(entity.getWindTurbineTotal());
            vo.setWindTurbineNormal(entity.getWindTurbineNormal());
            vo.setPvStationTotal(entity.getPvStationTotal());
            vo.setInverterNormal(entity.getInverterNormal());
        }
        
        return vo;
    }

}
